import 'dart:io';
import 'dart:convert' as convert;

import 'package:path_provider/path_provider.dart';

import '../util/log_util.dart';

class SettingAndFilterManager{

  SettingAndFilterManager._();

  static final SettingAndFilterManager _instance = SettingAndFilterManager._();
  static SettingAndFilterManager get instance => _instance;

  String filePath = '';
  bool isVersionCheck = false;
  String macCode = '';
  String creditCode = '';
  String addressFilter = '';
  String advNameFilter = '';
  bool isBondCheck = false;



  //存储过滤数据
  void writeDataToFile() {

    if(Platform.isAndroid){
      getExternalStorageDirectory().then((value) {
        _saveOtaDateToFileInternal(value!);
        _saveFilterDateToFileInternal(value!);
      });
    }else if(Platform.isIOS){
      getApplicationDocumentsDirectory().then((value) {
        _saveOtaDateToFileInternal(value);
        _saveFilterDateToFileInternal(value);
      });
    }else{
      throw UnsupportedError('not support platform ${Platform.localeName}');
    }
  }

  void _saveOtaDateToFileInternal(Directory value){
    Map<String, dynamic> map = {};
    map['filePath'] = filePath;
    map['isVersionCheck'] = isVersionCheck;
    map['macCode'] = macCode;
    map['creditCode'] = creditCode;
    map['isBondCheck'] = isBondCheck;

    _savePath('${value.path}/ota_words', convert.jsonEncode(map));
  }

  void _saveFilterDateToFileInternal(Directory value){
    Map<String, dynamic> map = {};
    map['addressFilter'] = addressFilter;
    map['advNameFilter'] = advNameFilter;

    _savePath('${value.path}/filter_words', convert.jsonEncode(map));
  }

  void _savePath(String path, String data){
    Log.d('setting and filter path:$path');
    File file = File(path);
    if(!file.existsSync()) {
      file.createSync(recursive: true);
    }

    file.writeAsString(data, flush: true);
    if(file.existsSync()) {
      Log.d('数据：$data保存成功');
    }
  }

  //读取指定文件中的数据
  Future<Map> getFilterWordsFromFile() async {
    Directory? documentsDir;//await getApplicationDocumentsDirectory();
    if(Platform.isAndroid){
      documentsDir = await getExternalStorageDirectory();
    }else if(Platform.isIOS){
      documentsDir = await getApplicationDocumentsDirectory();
    }else{
      throw UnsupportedError('not support platform ${Platform.localeName}');
    }
    String? documentsPath = documentsDir?.path;

    File file = File('$documentsPath/filter_words');
    if(!file.existsSync()) {
      return {};
    }

    String filterTxt = await file.readAsString();
    Map<String, dynamic> map = convert.jsonDecode(filterTxt);
    return map;

  }

  //读取指定文件中的数据
  Future<Map> getOtaWordsFromFile() async {
    Directory? documentsDir;//await getApplicationDocumentsDirectory();
    if(Platform.isAndroid){
      documentsDir = await getExternalStorageDirectory();
    }else if(Platform.isIOS){
      documentsDir = await getApplicationDocumentsDirectory();
    }else{
      throw UnsupportedError('not support platform ${Platform.localeName}');
    }
    String? documentsPath = documentsDir?.path;

    File file = File('$documentsPath/ota_words');
    if(!file.existsSync()) {
      return {};
    }

    String otaTxt = await file.readAsString();
    Map<String, dynamic> map = convert.jsonDecode(otaTxt);
    return map;

  }
}